(function(){
    angular.module('app').controller("agbRptResCtrl",function($scope,proxy){
        $scope.filter = {}

        $scope.isDirect = isDirect;
        $scope.platformList = [{name:'全部平台'},{id:'android',name:'Android'},{id:'ios',name:'iOS'}]
        $scope.chargeTypeList = [{name:'全部资源类型'},{id:'cpc',name:'CPC'},{id:'cpm',name:'CPM'}]
        $scope.dealTypeList = [{name:'全部采买类型'},{id:'pdb',name:'保价保量'},{id:'pd',name:'保价不保量'}]

        $scope.sTime = getNowFormatDate() + " 00:00:00";
        $scope.eTime = getNowFormatDate() + " 23:59:59";
        $scope.chooseDate = function (start, end) {
            if(start) {
                $scope.sTime = start + " 00:00:00";
                $scope.eTime = end + " 23:59:59";
            } else {
                $scope.sTime = $scope.eTime = undefined;
            }
            getList();
            getSum();
        }
        $scope.search = function(val) {
            $scope.filter.skuName = val;
            getList();
            getSum();
        }
        $scope.selectAdv = function(item) {
            $scope.filter.owner = item.id;
            getList();
            getSum();
        }
        $scope.selectPlatform= function(item) {
            $scope.filter.platform = item.id;
            getList();
            getSum();
        }
        $scope.selectChargeType = function(item) {
            $scope.filter.chargeType = item.id;
            getList();
            getSum();
        }
        $scope.selectDealType = function(item) {
            $scope.filter.dealType = item.id;
            getList();
            getSum();
        }

        // 下载报表
        $scope.download = function() {
            var data = {
                'page': "1",
                'size': "99999",
                'filter': $scope.filter,
                "sort": ["cTime,1"],
                sTime : $scope.sTime,
                eTime : $scope.eTime
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/agc/deal',
                load: true,
                data: data,
                success: function(res){
                    var arr = res.data.data || [];
                    arr.forEach(function (v) {
                        if(v.dealType == 'pdb') {
                            v.dealType = "保价保量";
                        } else {
                            v.dealType = "保价不保量";
                            v.price = v.advertiserProfit;
                        }
                        v.agencyProfitRate = v.agencyProfitRate*100+"%";
                        v.clickRate = v.clickRate*100+"%";
                    })
                    exportCsv({
                        title: ['合约资源名称','采买类型','平台类型','资源类型','投放时段','花费(元)','成本(元)','利润(元)','利润率','展示量','点击量','点击率','单价'],
                        titleForKey: ['skuName','dealType','platformTag','chargeType','startDate','advertiserCost','agencyCost','agencyProfit','agencyProfitRate','view','click','clickRate','price'],
                        data: arr,
                    },"合约资源.csv");
                }
            })
        }

        getList();
        getSum();
        getAdvList();

        function getList(current, pagesize) {
            var data = {
                'page': (current || 1)+"",
                'size': (pagesize || 20)+"",
                'filter': $scope.filter,
                "sort": ["cTime,1"],
                sTime : $scope.sTime,
                eTime : $scope.eTime
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/agc/deal',
                load: true,
                data: data,
                success: function(res){
                    // 获得列表
                    $scope.list = res.data.data;
                    $("#pageToolbar").html("");
                    $('#pageToolbar').Paging({
                        pagesize: pagesize || '20',
                        current: current || '1',
                        count:res.data.total,
                        toolbar:true,
                        hash: false,
                        callback:getList
                    });
                }
            })
        }

        function getSum() {
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/agc/deal/sum',
                load: true,
                data: {
                    'page': "1",
                    'size': "999999",
                    'filter': $scope.filter,
                    sTime : $scope.sTime,
                    eTime : $scope.eTime
                },
                success: function(res){
                    // 获得列表
                    $scope.total = res.data.data;
                }
            })
        }


        // 广告主
        function getAdvList() {
            var data = {
                'page': "1",
                'size': "9999",
                "sort": ["cTime,1"],
                "filter": $scope.filter
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/advertiser/putin/list',
                load: true,
                data: data,
                success: function(res){
                    // 获得列表
                    $scope.advList = res.data.data;
                    $scope.advList.unshift({name:"全部广告主"})
                }
            })
        }
    })
})();